package com.service.impl;

import com.Dto.CounselorInfoDto;
import com.Dto.GuidanceRecordDto;
import com.mapper.*;
import com.pojo.Counselor;
import com.pojo.GuidanceSession;
import com.pojo.Supervisor;
import com.service.AdminService;
import com.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
通讯相关功能
 */
@Service
public class MessageServiceImpl implements MessageService {


    @Autowired
    private CounselingSessionMapper counselingSessionMapper;

    @Autowired
    private GuidanceSessionMapper guidanceSessionMapper;

    @Autowired
    private GuidanceMessageMapper guidanceMessageMapper;

    @Autowired
    private CounselorMapper counselorMapper;

    @Autowired
    private SupervisorMapper supervisorMapper;


    //创建一个新的咨询会话
    public int createCounselingSession(Integer visitorId, Integer counselorId,LocalDateTime currentTime,Integer state){
        int sessionId = counselingSessionMapper.createCounselingSession(visitorId,counselorId,currentTime);
        return sessionId;
   }

    //结束咨询会话
    public int endCounselingSession(Integer visitorId, Integer counselorId,LocalDateTime currentTime){
        int sessionId = counselingSessionMapper.endCounselingSession(visitorId,counselorId,currentTime);
        return sessionId;
   }

    //创建一个新的求助会话
    public int createGuidanceSession(Integer counselorId, Integer supervisorId, LocalDateTime currentTime,Integer state){
        Counselor counselor = counselorMapper.findCounselorByUserId(counselorId);
        Supervisor supervisor = supervisorMapper. findSupervisorByUserId(supervisorId);
        guidanceSessionMapper.createNewGuidanceSession(counselor.getId(),supervisor.getId(),currentTime,state);
        GuidanceSession guidanceSession = guidanceSessionMapper.getOriginalData(counselor.getId(),supervisor.getId());
        return guidanceSession.getId();
    }

    //结束求助会话
    public int endGuidanceSession(Integer counselorId, Integer supervisorId,LocalDateTime currentTime){

        Counselor counselor = counselorMapper.findCounselorByUserId(counselorId);
        Supervisor supervisor = supervisorMapper. findSupervisorByUserId(supervisorId);
        GuidanceSession guidanceSession = guidanceSessionMapper.getOriginalData(counselor.getId(),supervisor.getId());
        //计算duration，先获取最初时间
        LocalDateTime beginTime = guidanceSession.getTime();
        Integer sessionId = guidanceSession.getId();
        System.out.print(sessionId);
        Duration duration = Duration.between(beginTime, currentTime);

        System.out.print(duration);

        long seconds = duration.getSeconds();
        int hours = (int) (seconds / 3600);
        int minutes = (int) ((seconds % 3600) / 60);
        int remainingSeconds = (int) (seconds % 60);

        LocalTime localTimeDuration = LocalTime.of(hours, minutes, remainingSeconds);

        guidanceSessionMapper.addDuration(sessionId,localTimeDuration);
        guidanceSessionMapper.updateState(sessionId);
        return sessionId;
    }

    //接收单条消息
    public boolean receiveSingleMessage(Integer from, Integer to, Integer sessionId, String content, LocalDateTime time, Integer type){
        boolean flag;
        //如果是咨询师和督导传
        if(type == 0 ) {
            Counselor counselor1 = counselorMapper.findCounselorByUserId(from);
            Supervisor supervisor1 = supervisorMapper.findSupervisorByUserId(to);
            flag = guidanceMessageMapper.addSingleMessage(counselor1.getId(), supervisor1.getId(), sessionId, content, time, type);

        }else{
            Counselor counselor2 = counselorMapper.findCounselorByUserId(to);
            Supervisor supervisor2 = supervisorMapper.findSupervisorByUserId(from); //from是督导
            flag = guidanceMessageMapper.addSingleMessage(supervisor2.getId(), counselor2.getId(), sessionId, content, time, type);
        }
        return flag;

    }

    //查询求助记录
    public List<GuidanceRecordDto> getGuidanceRecords(Integer counselorId, Integer supervisorId, LocalDateTime time){

        Counselor counselor = counselorMapper.findCounselorByUserId(counselorId);
        Supervisor supervisor = supervisorMapper.findSupervisorByUserId(supervisorId);

        Integer sessionId= guidanceSessionMapper.findSessionId(counselor.getId(),supervisor.getId(),time);

        List<GuidanceRecordDto> messageList = guidanceMessageMapper.findBySessionId(sessionId);
        return messageList;

    }




}
